var express = require('express');
var router = express.Router();

var conn = require("../setting");


/* GET home page. */
router.get('/', function(req, res, next) {
	console.log("首页："+req.query.id)

	conn.query('select * ,articles.id as artid from articles join catalog on articles.cata_id = catalog.id order by artid desc limit 10', function(err, rows, fields) {
		var items = rows;
		if (err) throw err;
		conn.query('select * from catalog', function(err, rows, fields) {
			var navs = rows;
			if (err) throw err;
 			res.render('index', { title: 'jyouh',items:items,navs:navs ,cata_id:0});
 		}) 
	});
});


router.get('/news', function(req, res, next) {
	conn.query('select *,articles.id as artid from articles join catalog on articles.cata_id = catalog.id where cata_id='+req.query.id+' limit 10', function(err, rows, fields) {
		var items = rows;
		if (err) throw err;
		conn.query('select * from catalog', function(err, rows, fields) {
			var navs = rows;

			if (err) throw err;
 			res.render('list', { title: 'jyouh',items:items,navs:navs,cata_id:req.query.id });
 		}) 
	});
});




router.get('/articles',function(req,res,next){
	conn.query('select * from catalog', function(err, rows, fields) {
		var navs = rows;
		if (err) throw err;

		conn.query('select * from articles where id ='+req.query.id,function(err,rows,fields){
			if (err) throw err;
			console.log(rows[0])
			res.render('articles', { title: 'jyouh',navs:navs,article:rows[0],cata_id:rows[0].cata_id});
		})

	})

})




router.get('/form', function(req, res, next) {
res.render('form', { title: 'jyouh' });
})

module.exports = router;
